package com.example.game_forum.controller;

import com.example.game_forum.dto.TPostContentDto;
import com.example.game_forum.dto.TPostDto;
import com.example.game_forum.dto.TPostTitleDto;
import com.example.game_forum.service.TPostService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@AllArgsConstructor
@RestController
@RequestMapping("/userapi/t-post")
@Tag(name = "用户帖子管理", description = "用户帖子接口")
public class TPostController {
    private final TPostService postService;

    @Operation(
            summary = "发布新帖子",
            description = "创建并发布一个新的帖子"
    )
    @ApiResponse(
            responseCode = "200",
            description = "成功发布帖子",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = TPostDto.class)
            )
    )
    @ApiResponse(
            responseCode = "400",
            description = "请求参数错误",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = String.class)
            )
    )
    @PostMapping("/add")
    public ResponseEntity<Void> addPost(@Valid @RequestBody TPostDto postDto) {
        postService.addPost(postDto);
        return ResponseEntity.ok().build();
    }

    @Operation(
            summary = "更新帖子",
            description = "根据帖子ID更新帖子内容"
    )
    @ApiResponse(
            responseCode = "200",
            description = "成功更新帖子",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = String.class)
            )
    )
    @ApiResponse(
            responseCode = "400",
            description = "请求参数错误",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = String.class)
            )
    )
    @PutMapping("/update")
    public ResponseEntity<Void> updatePost(@RequestParam String postId, @Valid @RequestBody TPostDto postDto) {
        postService.updatePost(postId, postDto);
        return ResponseEntity.ok().build();
    }

    @Operation(
            summary = "删除帖子",
            description = "根据帖子ID删除帖子"
    )
    @ApiResponse(
            responseCode = "200",
            description = "成功删除帖子",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = String.class)
            )
    )
    @ApiResponse(
            responseCode = "400",
            description = "请求参数错误",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = String.class)
            )
    )
    @DeleteMapping("/delete")
    public ResponseEntity<Void> deletePost(@RequestParam String postId) {
        postService.deletePost(postId);
        return ResponseEntity.ok().build();
    }
}
